﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Testajaxfileupload
{
    public partial class upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpFileCollection files = Request.Files;
            string msg = string.Empty;
            string error = string.Empty;
            string imgurl;
            if (files.Count > 0)
            {
                string path = files[0].FileName;

                string name = Path.GetFileName(path);
                string extension = Path.GetExtension(path);

                string directPath = Server.MapPath("~/uploads/images");
                if (!Directory.Exists(directPath))
                {
                    Directory.CreateDirectory(directPath);
                }

                string newName = Guid.NewGuid().ToString() + extension;
                string savaPath = Path.Combine(directPath, newName);

                files[0].SaveAs(savaPath);

                msg = " 成功! 文件大小为:" + files[0].ContentLength;

                string res = "{ \"error\":\"" + error + "\", \"msg\":\"" + msg + "\",\"imgurl\":\"/uploads/images/" + newName + "\"}";
                Response.Write(res);
                Response.End();
            }

        }
    }
}